﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Northwind.Model;
using Northwind.Repositories;
using Northwind.Repositories.Entity;

namespace jQuery.UI.Grid.Stage1.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        #region Fields
        IProductsRepository _productsRepository;
        #endregion

        #region Constructors
        public HomeController()
            : this(new ProductsRepository())
        {
        }

        public HomeController(IProductsRepository productsRepository)
        {
            _productsRepository = productsRepository;
        }
        #endregion

        public ViewResult DataModel()
        {
            return View();
        }

        public JsonResult Products()
        {
            object[] products = (from product in _productsRepository.FindAll()
                                 select new
                                 {
                                    Id = product.Id,
                                    Name = product.Name,
                                    Supplier = product.Supplier.Name,
                                    Category = product.Category.Name,
                                    QuantityPerUnit = product.QuantityPerUnit,
                                    UnitPrice = product.UnitPrice,
                                    UnitsInStock = product.UnitsInStock
                                 }).ToArray();

            return Json(products, JsonRequestBehavior.AllowGet);
        }
    }
}
